<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图 -->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="goAddPage">添加商品</el-button>
                </el-col>
            </el-row>
            <!-- table表格区域 -->
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="商品价格(元)" prop="goods_price" width="105px"></el-table-column>
                <el-table-column label="商品重量" prop="goods_weight" width="80px"></el-table-column>
                <el-table-column label="创建时间" prop="add_time" width="170px">
                    <template slot-scope="scope">
                        {{scope.row.add_time|dateFilter}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180px">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditCate(scope.row.goods_id)">编辑</el-button>
                        <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeGoods(scope.row.goods_id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页导航 -->
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[2,4,5,6,8,10]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
        </el-card>
        <!-- 修改分类对话框 -->
        <el-dialog
        title="修改分类"
        :visible.sync="editCateVisible"
        width="50%">
        <el-form :model="editCateForm" :rules="editCateRules" ref="editCateRef" label-width="100px">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="editCateForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="editCateForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="editCateForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model="editCateForm.goods_weight"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editCateVisible = false">取 消</el-button>
          <el-button type="primary" @click="sunmitCateData">确 定</el-button>
        </span>
      </el-dialog>
    </div>
</template>
<script>
export default {
  data () {
    return {
      //   要请求的数据
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 获取到的数据
      goodsList: [],
      //   商品总数
      total: 0,
      editCateVisible: false,
      // 根据id获取的商品信息
      editCateForm: {
      },
      // 编辑商品的表单规则
      editCateRules: {
        goods_name: [
          { required: true, message: '请输商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输商品价格', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输商品数量', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输商品重量', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods',
        { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      console.log(res.data)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 每页显示数量变化
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 显示页码时间
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 删除商品
    async removeGoods (id) {
      const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getGoodsList()
    },
    // 添加商品页面跳转
    goAddPage () {
      this.$router.push('/goods/add')
    },
    // 显示编辑商品对话框
    async showEditCate (id) {
      const { data: res } = await this.$http.get('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('获取商品信息失败')
      this.editCateForm = res.data
      this.editCateVisible = true
    },
    // 提交商品
    sunmitCateData () {
      this.$refs.editCateRef.validate(async valid => {
        if (!valid) return this.$message.error('请填写相关信息')
        const { data: res } = await this.$http.put('goods/' + this.editCateForm.goods_id, this.editCateForm)
        if (res.meta.status !== 200) return this.$message.error('修改失败')
        this.getGoodsList()
        this.$message.success('修改成功')
        this.editCateVisible = false
      })
    }
  },
  created () {
    this.getGoodsList()
  }

}
</script>
<style lang="less" scoped>

</style>
